package com.xhd.server;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Vector;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.base.session.Session;
import com.xhd.base.util.xBaseObj;
import com.xhd.db.DaoMonitor;
import com.xhd.exception.SystemException;
import com.xhd.exception.XException;
import com.xhd.servlet.ClientAction;

public class ProcessHandleMgr {
	private int jm395 = 1;
	  private Vector jm396 = null;
	  private int jm397 = 0;
	  private HashMap jm398 = null;
	  private static final int jm224 = 1;
	  private static final int jm225 = 2;
	  private static ProcessHandleMgr jm16 = null;
	  private static ThreadLocal jm399 = new ThreadLocal();
	  private static final Log jm9;
	  private static DaoMonitor jm400;
	  static Class jm10;

	  static
	  {
	    Class tmp17_14 = jm10;
	    if (tmp17_14 == null)
	    {
	     
	      try
	      {
	       ProcessHandleMgr.jm10 = Class.forName("com.sinocc.base.server.ProcessHandleMgr");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp17_14);
	  }

	  private ProcessHandleMgr()
	  {
	    int i = ServerConfig.ThreadMax;
	    this.jm398 = new HashMap(i);
	    this.jm396 = new Vector(i);
	    jm400 = DaoMonitor.getInstance();
	    for (int j = 0; j < i; ++j)
	      this.jm396.add(new ProcessHandle());
	    this.jm397 = i;
	  }

	  private synchronized ProcessHandle jm394(int paramInt, ProcessHandle paramProcessHandle)
	    throws XException
	  {
	    if (1 == paramInt)
	    {
	      if (this.jm397 < 1)
	        throw new SystemException(2);
	      this.jm397 -= 1;
	      paramProcessHandle = (ProcessHandle)this.jm396.remove(0);
	      paramProcessHandle.setId(this.jm395);
	      this.jm398.put(String.valueOf(this.jm395), paramProcessHandle);
	      this.jm395 += 1;
	      if (this.jm395 > 9999999)
	        this.jm395 = 1;
	      return paramProcessHandle;
	    }
	    if (paramProcessHandle != null)
	    {
	      this.jm397 += 1;
	      this.jm396.add(paramProcessHandle);
	      this.jm398.remove(String.valueOf(paramProcessHandle.getId()));
	    }
	    return null;
	  }

	  public void closeHandle(ProcessHandle paramProcessHandle)
	  {
	    if (paramProcessHandle == null)
	      return;
	    try
	    {
	      jm394(2, paramProcessHandle);
	      paramProcessHandle.getAction().log();
	      int i = jm400.counterVal();
	      jm400.counterReset();
	      if (i <= 0)
	        return;
	      String str = paramProcessHandle.getAction().getFullName();
	      str = str + " has database connection not free: " + i;
	      jm9.error(str);
	    }
	    catch (Exception localException)
	    {
	      jm9.error(localException);
	    }
	  }

	  public static synchronized ProcessHandleMgr getInstance()
	  {
	    if (jm16 == null)
	      jm16 = new ProcessHandleMgr();
	    return jm16;
	  }

	  public static ProcessHandle openHandle(ClientAction paramClientAction, Session paramSession)
	    throws XException
	  {
	    ProcessHandle localProcessHandle = getInstance().jm394(1, null);
	    localProcessHandle.setAction(paramClientAction);
	    localProcessHandle.setSession(paramSession);
	    paramClientAction.setProcessId(localProcessHandle.getId());
	    paramClientAction.setActionTime(localProcessHandle.getOpenTime());
	    jm399.set(localProcessHandle);
	    return localProcessHandle;
	  }

	  public static ProcessHandle openHandle(ClientAction paramClientAction, xBaseObj paramxBaseObj)
	    throws XException
	  {
	    ProcessHandle localProcessHandle = getInstance().jm394(1, null);
	    localProcessHandle.setAction(paramClientAction);
	    localProcessHandle.setBizObj(paramxBaseObj);
	    localProcessHandle.setSession(paramxBaseObj.getSession());
	    paramClientAction.setProcessId(localProcessHandle.getId());
	    paramClientAction.setActionTime(localProcessHandle.getOpenTime());
	    jm399.set(localProcessHandle);
	    return localProcessHandle;
	  }

	  public static void closeHandle()
	  {
	    getInstance().closeHandle((ProcessHandle)jm399.get());
	    jm399.set(null);
	  }

	  public static ProcessHandle getHandle()
	  {
	    return ((ProcessHandle)jm399.get());
	  }

	  public Hashtable getProcessInfo(int paramInt)
	  {
	    try
	    {
	      ProcessHandle localProcessHandle = (ProcessHandle)this.jm398.get(String.valueOf(paramInt));
	      if (localProcessHandle != null)
	        return localProcessHandle.getProcessInfo(true);
	    }
	    catch (Exception localException)
	    {
	      jm9.error(localException);
	    }
	    return null;
	  }

	  public int killProcess(int paramInt)
	  {
	    try
	    {
	      ProcessHandle localProcessHandle = (ProcessHandle)this.jm398.get(String.valueOf(paramInt));
	      if ((localProcessHandle != null) && (localProcessHandle.getBizObj() != null))
	        return localProcessHandle.getBizObj().getDaoTransaction().kill();
	    }
	    catch (Exception localException)
	    {
	      jm9.error(localException);
	    }
	    return 0;
	  }

	  public Vector getProcessList()
	  {
	    Vector localVector1 = new Vector();
	    if (this.jm398.size() < 1)
	      return null;
	    Vector localVector2 = new Vector(this.jm398.values());
	    int i = localVector2.size();
	    for (int j = 0; j < i; ++j)
	      try
	      {
	        ProcessHandle localProcessHandle = (ProcessHandle)localVector2.get(j);
	        if (localProcessHandle != null)
	          localVector1.add(localProcessHandle.getProcessInfo(false));
	      }
	      catch (Exception localException)
	      {
	        jm9.error(localException);
	      }
	    return localVector1;
	  }
}
